package com.sopaths.web.controller.search;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sopaths.contract.domain.SearchRecruitment;
import com.sopaths.contract.service.SearchService;

@Controller
@RequestMapping("/search")
public class SearchController {

	@Inject
	private SearchService searchService;

	@RequestMapping(method = RequestMethod.GET)
	public String setupUserRegister(Model model) {
		return "index";
	}

	@RequestMapping(value = "/search_recruitment/{primaryLocation}/{jobTitile}", method = RequestMethod.GET)
	public @ResponseBody
	String searchRcruitment(@PathVariable String primaryLocation,
			@PathVariable String jobTitile) {
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("primaryLocation", "北京");
		parameters.put("jobTitile", "点");
		this.searchService.searchRecruitment(parameters);
		return "";
	}

	@RequestMapping(value = "/solrAjaxSearch", method = RequestMethod.GET)
	public String solrAjaxSearch(HttpServletRequest request,
			HttpServletResponse reponse) throws SolrServerException {
		String keyWord = request.getParameter("keyword");
		String workExperence = request.getParameter("workExperence");
		return "";
	}

	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public String query(HttpServletRequest request, HttpServletResponse reponse)
			throws SolrServerException {

		Map<String, String> parameters = new HashMap<String, String>();
		String keyWord = request.getParameter("keyWord");
		String searchType = request.getParameter("searchType");
		String sortColumn = request.getParameter("sortColumn");
		String sortType = request.getParameter("sortType");
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");

		parameters.put("jobTitile", keyWord);
		parameters.put("searchType", searchType);
		parameters.put("sortColumn", sortColumn);
		parameters.put("sortType", sortType);
		parameters.put("pageNo", pageNo);
		parameters.put("pageSize", pageSize);

		Map<String, Object> result = new HashMap<String, Object>();

		result = this.searchService.searchRecruitment(parameters);
		List<SearchRecruitment> recruiments = (List<SearchRecruitment>) result
				.get("recruiments");

		request.setAttribute("recruiments", recruiments);
		request.setAttribute("keyword", keyWord);

		return "search/result";
	}
}
